package com.eud.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eud.entity.User;

import com.eud.service.UserService;

import com.eud.service.impl.UserServiceImpl;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String username=request.getParameter("name");
		String userpass=request.getParameter("password");
		String repass=request.getParameter("repassword");
		String sex=request.getParameter("sex");
		String phone=request.getParameter("phonenum");
		String idcard=request.getParameter("idCard");
		UserService urs=new UserServiceImpl();
		User u1=new User();
		if(urs.query(username)!=null){
			 u1=urs.query(username);
		}
		User user=new User(username,userpass,sex,phone,idcard);
	
		if(user.getuName()!=null && user.getuPass()!=null && user.getuSex()!=null && user.getuNum()!=null && user.getuIdCard()!=null ){
			if( userpass.equals(repass)  &&  !(username.equals(u1.getuName()))) {
				urs.addUser(user);
				response.sendRedirect("login.jsp");
			}else{
				response.sendRedirect("register.jsp");
			}
		}else{
			System.out.print(111111111);
			response.sendRedirect("register.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
